<?php
include_once("../inc/db.php");

// connect to database
$settings["server_mysql"] = "localhost";
db_init();
?>

<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript"> 
	google.load('visualization', '1', {packages: ['table']});

	// array containing all data used by google api visualization tables
	var tables = new Array();
</script>

<!-- include data for each table -->
<?php include "incTableStalePending.php" ?>
<?php include "incTableStaleActivity.php" ?>
<?php include "incTableExceptionCurrent.php" ?>
<?php include "incTableExceptionExpired.php" ?>

<script>
	// displays tables where ever they have been specified
	// tables are specified by a div tag with id='name_of_the_table'
	function drawTables() {
		// each array element of tables has:
		// 0 - name of the table
		// 1 - table data ready for the google api
		// 2 - doesn't exist yet, but will be the table instance we create
		for(var ctr = 0; ctr < tables.length; ctr ++) {
			var next = tables[ctr];

			// Create and draw the visualization.
			next[2] = new google.visualization.Table(document.getElementById(next[0]));
			next[2].draw(next[1], null);
		}
	}
    
/***
 * Example function which will be merged with Process Request
 *
 */
/*	function trigger() {
      if(table == null) {
        alert("table get failed");
        return;
      }
      
      var sortinfo = table.getSortInfo();
      if(sortinfo == null) {
        return;
      }
      
      var selected = table.getSelection();
      if(selected == null) {
        return;
      }
      
      alert(selected.length + " rows selected");
      for(var ctr = 0; ctr < selected.length; ctr ++) {
        var row = selected[ctr].row;
        var item = data.getValue(row, 0);
        alert("Row " + row + " at column 0 is " + item);
      }
    }
*/
      
    google.setOnLoadCallback(drawTables);
</script>

<!--
<script type="text/javascript" src="http://tloyd-ser/common/tabber.js"></script>
<link rel="stylesheet" href="http://tloyd-ser/common/tabber.css" TYPE="text/css" MEDIA="screen">
-->
<script type="text/javascript"> 
<?php include "tabber.js" ?>
</script>
<style>
<?php include "tabber.css" ?>
</style>


<script type="text/javascript"> 
//
function processRequest(max) {
	var d = new Date();
	var count = 0;
	var table = null;
	var found = false;


	for(var ctr = 0; ctr < tables.length; ctr ++) {
		var next = tables[ctr];
		if(next[0] == "table_stale_pending") {
			found = true;
			break;
		}
	}

	if(!found) {
		return;
	}

	var selected = next[2].getSelection();
//	alert(selected.length + " rows selected");

	var ta = document.getElementsByName("ta_request")[0];
	var username = document.getElementsByName("username")[0];
	ta.value = "To: <?php $detail = parse_ini_file("detail.ini"); echo $detail['contact']; ?>\n"
			+ "Subject: AD Cleanup Exception Request (Temporary)\n"
			+ "\n"
			+ "I understand:\n"
			+ "\n"
			+ "1. These AD Objects have lost domain trust and adding an exception will *not* change that\n"
			+ "2. If the machine is online and pings, this activity will be detected, and it will be removed\n"
		    + "   from the pending list automatically\n"
			+ "3. The way to resolve lost domain trust is to call x83000 and ask them to have the machine\n"
		    + "   rejoined to the domain.\n"
			+ "4. This exception is temporary and will expire after 180 days unless a permanent exception is\n"
		    + "   specifically requested.\n"
			+ "\n";

	for(var ctr = 0; ctr < selected.length; ctr ++) {
		var row = selected[ctr].row;
		var item = next[1].getValue(row, 0);
//		alert("Row " + row + " at column 0 is " + item);
		ta.value += 'NULL,"' + item +'","' + username.value
				+ '","'
				+ d.getFullYear() +"-"+ (d.getMonth()+1) +"-"+ d.getDate()
				+ '",180,"",1,0' + '\n';
	}

	if(username.value.length == 0) {
		alert("Please specify your username.");
	}
	else {
		if(selected.length == 0) {
			alert("At least one AD object must be selected.");
		}
		else {
			// display the formatted email window
			var item = document.getElementById("topic_request");
			if(document.getElementById("topic_request").style.display == "none") {
				toggle_hide("topic_request");
			}
		}
	}
}

// select tab in the specified topic
function setTab(topic_id, tab_id) {
	if(topic_id == "topic_stale") {
		if(tab_id == "tab_pending") {
			if(document.getElementById("tab_pending").style.display == "none")
					toggle_hide("tab_pending");
			if(document.getElementById("tab_activity").style.display == "inline-block")
					toggle_hide("tab_activity");
		}
		else if(tab_id == "tab_activity") {
			if(document.getElementById("tab_activity").style.display == "none")
					toggle_hide("tab_activity");
			if(document.getElementById("tab_pending").style.display == "inline-block")
					toggle_hide("tab_pending");
		}
	}
	else if(topic_id == "topic_exception") {
		if(tab_id == "tab_current") {
			if(document.getElementById("tab_current").style.display == "none")
					toggle_hide("tab_current");
			if(document.getElementById("tab_expired").style.display == "inline-block")
					toggle_hide("tab_expired");
		}
		else if(tab_id == "tab_expired") {
			if(document.getElementById("tab_expired").style.display == "none")
					toggle_hide("tab_expired");
			if(document.getElementById("tab_current").style.display == "inline-block")
					toggle_hide("tab_current");
		}
	}
}
 
// toggle visibility of a div section
function toggle_hide(div_id) {
	var item = document.getElementById(div_id);
	if(item.style.display == "inline-block") {
		item.style.display = "none";
	}
	else {
		item.style.display = "inline-block";
	}
}
</script>
</head>
<body style="margin:0;padding:0" bgcolor='lightgray'>

<table border='0' width='100%'>
<tr><td colspan='2' align='center' bgcolor='white'> 
<!-- upper display -->
<h2>Active Directory Cleanup Report</h2>
<tr><td valign='top' bgcolor='d9e9ff' align='center' width='225'>
<!-- side bar -->
<?php echo date("F j, Y", time()); ?>
<table cellspacing='0' border='0' width='200'>
<tr bgcolor='547db3'>
	<td rowspan='8'>
	<td colspan='2' align='center' width='100%'><font color='white'>Counts
	<td rowspan='8'>
<?php include "incCounts.php"; ?>
</table>

	<p>

<?php include "incNotes.php"; ?>

<td bgcolor='white' valign='top' width='100%'>
	<!-- topic display -->
<div class="tabber">
	<div class="tabbertab" title="Instructions">
<?php include "incInstructions.php"; ?>
	</div>	<!-- end tabbertab -->


	<!-- Topic: Stale -->
	<div class="tabbertab" title="Stale">
	<table cellspacing='0' border='0' width='100%'><tr><td>
		<a href="javascript:setTab('topic_stale', 'tab_pending')">Pending Deletion</a>
		| <a href="javascript:setTab('topic_stale', 'tab_activity')">Activity Detected</a>
		<p>
		<div id='tab_pending' style='display: inline-block;'>

<font size='4'><b>Pending Deletion</b></font>
<p>
List of active directory objects being monitored for activity.  If there are
no signs of activity by the end of the month, these objects will be deleted.
<p>

<form>
<div id='topic_request' style='display: none'>
	Formatted Email Request: (<i>Copy this into an email and send.</i>)<br>
	<textarea name='ta_request' rows='15' cols='100' readOnly='true'/></textarea>
	<br>
	<input type='button' value='Hide Request' onClick="javascript:toggle_hide('topic_request')"/>
	<p>
</div>

<?php
echo
"Username: <input name='username' type='text' value=''/> <input type='button' onClick=\"javascript:processRequest($max)\" value='Request Exception'/>
<p>";
?>
<div id="table_stale_pending"></div>
</form>

		</div>
		<div id='tab_activity' style='display: none;'>

<font size='4'><b>Activity Detected</b></font>
<p>
List of active directory objects which were being monitored for activity.  Activity
was detected, and they have been removed from the pending cleanup.
<p>

<div id="table_stale_activity"></div>

		</div>
	</table>

	</div>	<!-- end tabbertab -->

	<!-- Topic: Exceptions -->
	<div class="tabbertab" title="Exceptions">
	<table cellspacing='0' border='0' width='100%'><tr><td>
		<a href="javascript:setTab('topic_exception', 'tab_current')">Current</a>
		| <a href="javascript:setTab('topic_exception', 'tab_expired')">Expired</a>
		<p>
		<div id='tab_current' style='display: inline-block;'>
<font size='4'><b>Current</b></font>
<p>
List of patterns which, if an object matches the pattern, is removed from the pending
cleanup. Exceptions expire after the specified number of days, or never if days=0.
<p>

<div id="table_exception_current"></div>

		</div>
		<div id='tab_expired' style='display: none'>
<font size='4'><b>Expired</b></font>
<p>
List of patterns which have expired and are no longer involved in the cleanup process.
<!--
<p>
<b><font color='green'>
Note:  Expiration dates of exceptions is a new feature and is not currently active.
All requested exceptions are still currently in use.
</font></b>
-->
<p>

<div id="table_exception_expired"></div>

		</div>
	</table>

	</div>	<!-- end tabbertab -->
</div>	<!-- end tabber -->

</table>

</body>
</html>
